// 发布-订阅
export class EventEmitter<T> {
  private subscribers: Array<(data: T) => void> = []

  subscribe(callback: (data: T) => void) {
    this.subscribers.push(callback)
  }

  unsubscribe(callback: (data: T) => void) {
    this.subscribers = this.subscribers.filter(
      (subCallback) => subCallback !== callback,
    )
  }

  publish(data: T) {
    this.subscribers.forEach((callback) => callback(data))
  }
}
